package ui.graphical;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class UIHelper {
	public static boolean confirm(String text) {
		return (JOptionPane.showConfirmDialog(null, text, "Let op:", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION);
	}
	public static void errorMsg(String error) {
		JOptionPane.showMessageDialog(null, error);
	}
	public static void centreerFrame(JFrame frame) {
		// centreer window, maar y moet minimaal 175 zijn om hoofdmenu vrij te houden
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds((dim.width-frame.getWidth())/2,Math.max((dim.height-frame.getHeight())/2, 175),frame.getWidth(),frame.getHeight());
	}
	public static String date2string(Date date) {
		if(date!=null) {
			SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy");
			return sd.format(date);
		}
		return null;
	}
	public static Date string2date(String text) throws SQLException {
		if(text.length()!=0) {
			SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy");
			try {
				return sd.parse(text);
			} catch (ParseException e) {
				throw new SQLException("Datum moet het formaat dd-mm-jjjj hebben");
			}
		}
		return null;
	}
}
